package com.snda.game.legend.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.snda.game.legend.base.DateUtils;
import com.snda.game.legend.entiy.Legend;
import com.snda.game.legend.enums.TopState;

@Repository
public class LegendDao extends BaseDao<Legend> {

    public List<Legend> queryCurrentServers() {
        Session session = getSession();
        Criteria criteria = session.createCriteria(Legend.class);
        Date now = new Date();
        criteria.add(
                Restrictions.or(
                        Restrictions.gt("top", TopState.normal.getState()),
                        Restrictions.ge("opentime", now)))
                .add(Restrictions.lt("opentime",
                        DateUtils.getThreeDaysAfterTime(now)))
                .addOrder(Order.desc("type")).addOrder(Order.desc("top"))
                .addOrder(Order.desc("weight")).addOrder(Order.asc("opentime"));
        List<Legend> servers = query(criteria);
        releaseSession(session);
        return servers;
    }

    public List<Legend> queryNextServers() {
        Session session = getSession();
        Criteria criteria = session.createCriteria(Legend.class);
        Date now = new Date();
        criteria.add(
                Restrictions.or(
                        Restrictions.gt("top", TopState.normal.getState()),
                        Restrictions.ge("opentime", now)))
                .addOrder(Order.desc("type")).addOrder(Order.desc("top"))
                .addOrder(Order.desc("weight")).addOrder(Order.asc("opentime"));
        List<Legend> servers = query(criteria);
        releaseSession(session);
        return servers;
    }

    public List<Legend> queryBeforeServers() {
        Session session = getSession();
        Criteria criteria = session.createCriteria(Legend.class);
        Date now = new Date();
        criteria.add(
                Restrictions.and(
                        Restrictions.eq("top", TopState.normal.getState()),
                        Restrictions.lt("opentime", now)))
                .addOrder(Order.desc("type")).addOrder(Order.desc("weight"))
                .addOrder(Order.desc("opentime"));
        List<Legend> servers = query(criteria);
        releaseSession(session);
        return servers;
    }

    public List<Legend> queryPerviousServers() {
        Session session = getSession();
        Criteria criteria = session.createCriteria(Legend.class);
        Date now = new Date();
        criteria.add(
                Restrictions.and(
                        Restrictions.eq("top", TopState.normal.getState()),
                        Restrictions.lt("opentime", now)))
                .add(Restrictions.gt("opentime",
                        DateUtils.getThreeDaysBeforeTime(now)))
                .addOrder(Order.desc("type")).addOrder(Order.desc("weight"))
                .addOrder(Order.desc("opentime"));
        List<Legend> servers = query(criteria);
        releaseSession(session);
        return servers;
    }

    public void nextRound() {
        // Session session = getSession();
        // Criteria criteria = session.createCriteria(Legend.class);
        // criteria.addOrder(Order.asc("opentime"));
        // criteria.setMaxResults(1);
        // List<Legend> servers = query(criteria);
        // if (servers != null && servers.size() > 0) {
        // Legend legend = servers.get(0);
        // Date openTime = legend.getOpentime();
        // criteria = session.createCriteria(Legend.class);
        // criteria.add(Restrictions.ge("opentime", openTime)).add(
        // Restrictions.lt("opentime", DateUtils.getBeginTime()));
        // servers = query(criteria);
        // openTime = DateUtils.getEndTime();
        // for (Legend server : servers) {
        // server.setOpentime(openTime);
        // update(server);
        // }
        // }
        // releaseSession(session);

    }

    public void expired() {
        Session session = getSession();
        Criteria criteria = session.createCriteria(Legend.class);
        Date now = new Date();
        criteria.add(Restrictions.le("topbegintime", now)).add(
                Restrictions.ge("topendtime", now));
        List<Legend> nextTops = query(criteria);
        for (Legend top : nextTops) {
            top.setTop(TopState.top1.getState());
            update(top);
        }
        criteria = session.createCriteria(Legend.class);
        criteria.add(Restrictions.le("topendtime", now));
        List<Legend> previousTops = query(criteria);
        for (Legend top : previousTops) {
            top.setTop(TopState.normal.getState());
            update(top);
        }
        releaseSession(session);
    }

}
